package com.sk.places.places2.presentation.action;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.sk.places.places2.fb.service.FBService;

/**
 * Gets invoked when 'Login Using Facebook' button is clicked. 
 * @author skhurana
 *
 */
public class AuthAction implements 
				ServletRequestAware,ServletResponseAware   {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(getClass().getName()) ;
	//TODO - Use IOC i.e. spring
	private HttpServletRequest request ;
	private HttpServletResponse response ; 
	private FBService service  ; 
	private String username;
	private String buttonName ; 
 
	/*public AuthAction(FBService service) { 
		this.service = service ; 
	}
 */
	// all struts logic here
	public String execute() {
		try {
			System.out.println("entered authaction. Button nam");
			logger.info(" entered authaction. Button name " + buttonName) ;
			if(buttonName != null && buttonName.equals("testLogin")) {
				logger.info("testlogin clicked") ;
			}
			String redirectUrl = service.getLoginRedirectURL() ;
			logger.info("redirectUrl is  " + redirectUrl) ;
			//ActionContext.getContext().
			response.sendRedirect(redirectUrl) ; 
			
			System.out.println("Success : " + redirectUrl);
			return "SUCCESS";
		} 
		catch(Exception e) { 
			System.out.println("Exception");
			e.printStackTrace();
			logger.severe(e.getMessage()) ;
			return "error" ; 
		}
	}
	
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response ; 
		
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request ; 
		
	}

	public String getButtonName() {
		return buttonName;
	}

	public void setButtonName(String buttonName) {
		this.buttonName = buttonName;
	}

	public FBService getService() {
		return service;
	}

	public void setService(FBService service) {
		this.service = service;
	}

}
